<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    #content{
        border: 2px dashed #000;
        height: 100px;
    }
</style>
<body>
    <a href="/news">新闻</a>
    <a href="/home">主页</a>
    <div id="content"></div>
</body>
</html>
<script>
   // history

   //使用了 H5新增的history对象的方法 pushState replaceState

   //调用方法的时候，浏览器中的地址会改变，但不会包含在http请求里面

   //监听变化的方法 onpopstate

   //影响最大的区别就是 非常容易404

   //原因：刷新页面会把地址栏的地址当成请求地址，携带在http，发起请求，服务器不会响应就404

   window.onpopstate = function(e){
    console.log(123);
    console.log(e);
    content.innerHTML = `<h1>${e.state.content}</h1>`
    switch(content){
            case "/news":
                content.innerHTML = "<h1>新闻</h1>"
                break
            case "/home":
                content.innerHTML = "<h1>主页</h1>"

        }
   }

   let as = document.querySelectorAll('a')

   as.forEach(a => {
    a.onclick= function(e){
        e.preventDefault();
        console.log(this.getAttribute('href'));

        let path = this.getAttribute('href')

        history.pushState({content:this.innerText},null,path)

        switch(path){
            case "/news":
                content.innerHTML = "<h1>新闻</h1>"
                break
            case "/home":
                content.innerHTML = "<h1>主页</h1>"

        }
    }
   })

</script>